//
//  SYTabbarView.m

#import "SYTabbarView.h"

@implementation SYTabbarView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //自动伸缩高度可伸缩，右边距可以伸缩
       self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
        //设置背景图片
        self.backgroundColor= [UIColor whiteColor];
        //添加item
        [self addTabItems];
        
    }
    return self;
}

//设置默认选中的界面  在设置代理之后调用之后
-(void)selectItem{
    //设置默认选中第一个item
    SYTabbarItem *item = [self viewWithTag:10086 + 0];
    [self tabItemTouchEvent:item];
}

//添加Tab选项卡
- (void)addTabItems
{
    //课程
    [self addSingleTab:@"newnav_课程表_灰" selectedImage:@"newnav_课程表" weight:0 name:@"我" isBigger:NO];
    //直播
    [self addSingleTab:@"newnav_直播_灰" selectedImage:@"newnav_直播" weight:1 name:@"你" isBigger:NO];
    //题库
    [self addSingleTab:@"newnav_题库_灰" selectedImage:@"newnav_题库" weight:2 name:@"他" isBigger:NO];

}

- (void)addSingleTab:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage weight:(int)weight name:(NSString *)name isBigger:(BOOL)isBigger
{
    SYTabbarItem *tabItem=[[SYTabbarItem alloc]init];
    //设置图片  选择/未选
    [tabItem setBackroundImg:backgroundImage selectImg:selectedImage];
    //设置文字
    [tabItem setTitleText:name];
    //是否放大
    [tabItem setIconFrameWithIsBigger:isBigger];
    //设置位置
    tabItem.frame = CGRectMake(0, 70 + 100 * (weight), 60, 90);
    //设置选中触摸选中事件
    [tabItem addTarget:self action:@selector(tabItemTouchEvent:) forControlEvents:UIControlEventTouchDown];
    tabItem.tag = weight + 10086;
    [self addSubview:tabItem];
    
}


//设置触摸事件
- (void)tabItemTouchEvent:(SYTabbarItem *)tabItem
{
    if ([self.dockDelegate respondsToSelector:@selector(switchMainByTabItem:originalTab:destinationTab:)]) {
        [self.dockDelegate switchMainByTabItem:self originalTab:selectedTabItem.tag destinationTab:tabItem.tag];
    }
    selectedTabItem.enabled = YES;
    tabItem.enabled = NO;
    //将当前选中的赋值
    selectedTabItem = tabItem;
    
    for (int i = 0 + 10086 ; i < 3 + 10086; i++) {
        SYTabbarItem *allTabItem = [self viewWithTag:i];
        [allTabItem setUnSelected];
    }
    [tabItem setSelected];
}




@end
